package com.mzy.linear_struct.hot100.二叉树;

/**
 * @Author: mizy3
 * @Date: 2024/08/21/下午9:39
 * @Description:
 */
public class Pro108将有序数组转换为二叉搜索树 {
    public TreeNode sortedArrayToBST(int[] nums) {

        // 1 2 3 4 5

        //     4
        //  3     2
        // 1 5
        TreeNode tree = convertBSTree(nums, 0, nums.length - 1);
        return tree;
    }

    private TreeNode convertBSTree(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }

        int mid = (left + right) / 2;
        int num = nums[mid];
        TreeNode tree = new TreeNode(num);
        tree.left = convertBSTree(nums, left, mid - 1);
        tree.right = convertBSTree(nums, mid + 1, right);
        return tree;
    }

    public static void main(String[] args) {
        new Pro108将有序数组转换为二叉搜索树().sortedArrayToBST(new int[]{-10,-3,0,5,9});
    }
}





























